'''
fenlei = <div class="category-filter-type-name" data-code="yinyue">音乐</div><
yinyue = <a data-code="liuxing" href="/yinyue/liuxing/">流行</a>
shengyu = <a data-id="159" href="/yinyue/mr15t159/">运动</a>
<a class="active" aria-current="true" href="/yinyue/">综合排序</a>
<a href="/yinyue/mostplays/">播放最多</a>
<a class="HxMH album-title lg" title=".*?" href="(.*?)">(.*?)</a>
'''

import requests
import re
class Music:
    header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'}
    
    def __init__(self,
                 url = 'https://www.ximalaya.com/yinyue/',
                 charset = 'utf-8'):
        self.url = url
        self.charset = charset
        self.s = requests.Session()
        self.s.headers.update(self.header)
        self.info_html = self.html()
        self.music_info_list = self.pipei()
    def html(self):
        info_res = self.s.get(self.url)
        info_res.encoding = self.charset
        info_html = info_res.text
        return info_html
    def pipei(self):
        '''
         return [('/yinyue/liuxing/', '流行'), ('/yinyue/yaogun/', '摇滚'),...]   
        '''
        music_leixing_re = '<a data-code=".*?" href="(.*?)">(.*?)</a>'
        music_qita_re = '<a data-id=".*?" href="(.*?)">(.*?)</a>'
        
        music_leixing = re.findall(music_leixing_re,self.info_html)
        music_qita = re.findall(music_qita_re,self.info_html)
        
        music_leixing.extend(music_qita)
        music_dict = dict(music_leixing)
        #print(music_dict)
        
        return music_dict
    
       

                
    

if __name__ == '__main__':
    m = Music().everykinds()













        

